import requests
import pymongo
from datetime import datetime, timedelta
import time
import json

ALPHA_DB_HOST = 'mongodb://root:Jiangtun123Abc@dds-2vc48500d3766f641991-pub.mongodb.cn-chengdu.rds.aliyuncs.com:3717,dds-2vc48500d3766f642154-pub.mongodb.cn-chengdu.rds.aliyuncs.com:3717/warehouse_workflow?authSource=admin'
ALPHA_DB_PORT = 3717


def get_today_start_timestamp():
    # 获取当前时间
    now = datetime.now()
    # 获取当天0点时间
    today_start = datetime(now.year, now.month, now.day)
    # 转换为时间戳（秒为单位）
    timestamp = time.mktime(today_start.timetuple())
    return int(timestamp) * 1000


def get_monday_timestamp():
    # 获取当前日期
    now = datetime.now()

    # 计算当前是周几
    weekday = now.weekday()

    # 计算本周周一的日期
    monday = now - timedelta(days=weekday)

    # 设置时间为零点
    monday_midnight = monday.replace(hour=0, minute=0, second=0, microsecond=0)

    # 获取时间戳
    monday_midnight_timestamp = int(monday_midnight.timestamp())
    return monday_midnight_timestamp * 1000


jw = pymongo.MongoClient(ALPHA_DB_HOST, ALPHA_DB_PORT).jiwu
today_nums = jw.flash_flaw_return.count_documents({'finishTime': {'$gt': get_today_start_timestamp()}})
week_nums = jw.flash_flaw_return.count_documents({'finishTime': {'$gt': get_monday_timestamp()}})
r = requests.post('https://open.feishu.cn/open-apis/bot/v2/hook/3f4e1301-f114-4b35-99e4-e6264a9c2a3a',
                  json={"msg_type": "text",
                        "content": json.dumps({"text": f"今日质检上架数量：{today_nums}，本周质检上架数量：{week_nums}"})})
print(r.text)
